package com.fyakm.daodejing.simple;
import java.util.ArrayList;
import java.util.List;

public class Nature {
    private List<Species> speciesList; // 存储当前存在的所有物种

    public Nature() {
        this.speciesList = new ArrayList<>();
    }

    // 初始化物种的方法，在模拟开始时可以添加一些初始物种进来
    public void initializeSpecies(Species initialSpecies) {
        speciesList.add(initialSpecies);
    }

    // 推进大自然的发展，模拟一个时间阶段或者一轮进化过程，这里可以调用进化控制类的方法来让物种进化
    public void evolve(EvolutionController evolutionController) {
        List<Species> newGeneration = new ArrayList<>();
        for (Species species : speciesList) {
            Species evolvedSpecies = evolutionController.evolveSpecies(species);
            newGeneration.add(evolvedSpecies);
        }
        speciesList = newGeneration;
    }

    public void evolve(EvolutionController evolutionController, Environment environment) {
        List<Species> newGeneration = new ArrayList<>();
        for (Species species : speciesList) {
            double fitness = species.calculateFitness(environment);
            if (Math.random() < fitness) { // 根据适应度概率决定是否存活并繁殖
                Species evolvedSpecies = evolutionController.evolveSpecies(species);
                newGeneration.add(evolvedSpecies);
            }
        }
        speciesList = newGeneration;
    }




    // 获取当前所有物种的方法，方便查看模拟情况等
    public List<Species> getSpeciesList() {
        return speciesList;
    }
}
